package net.trevize.datasetexplorer.dialogs;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.regex.Pattern;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

import net.trevize.FSExplorer.FSTree.FSTree;
import net.trevize.IIDF.gui.ExploreIIDFPanel;
import net.trevize.datasetexplorer.DatasetExplorer;
import net.trevize.datasetexplorer.GalateePopupMenuInitializer;
import net.trevize.galatee.Galatee;
import net.trevize.gui.FSExplorer.FSE.FSEEvent;
import net.trevize.gui.FSExplorer.FSE.FSEListener;
import net.trevize.gui.layout.CellStyle;
import net.trevize.gui.layout.XGridBag;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * LDDTab3.java - May 17, 2010
 */

public class LDDTab3 extends JPanel implements FSEListener {

	private DatasetExplorer dataset_explorer;

	private FSTree fstree;

	public LDDTab3(DatasetExplorer dataset_explorer) {
		this.dataset_explorer = dataset_explorer;
		init();
	}

	private void init() {
		setLayout(new GridBagLayout());
		XGridBag xgb = new XGridBag(this);

		JLabel header = new JLabel();
		header.setOpaque(true);
		header.setBackground(Color.WHITE);
		header.setBorder(new MatteBorder(0, 15, 0, 0, Color.BLUE));
		header
				.setText("<html><body><h1>Load an IIDF dataset</h1></body></html>");
		CellStyle style0 = new CellStyle(1., 0., GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(header, style0, 0, 0, 1, 2);

		String file_filter = "(^[^\\.]$|^[^\\.].*[^~]$)";
		String dir_filter = "^[^\\.].*$";
		fstree = new FSTree(File.listRoots()[0], Pattern.compile(file_filter),
				Pattern.compile(dir_filter), true);
		fstree.addFSEListener(this);
		CellStyle style1 = new CellStyle(1., 1., GridBagConstraints.NORTHWEST,
				GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(fstree, style1, 1, 0, 1, 2);
	}

	/***************************************************************************
	 * implementation of FSEListener
	 **************************************************************************/

	@Override
	public void focusChanged(FSEEvent e) {
	}

	@Override
	public void open(FSEEvent e) {
		ExploreIIDFPanel explore_iidf_panel = new ExploreIIDFPanel(e
				.getSelectedFile().getPath());

		final Galatee galatee = explore_iidf_panel.getGalatee();

		GalateePopupMenuInitializer.initialize(galatee, dataset_explorer);

		dataset_explorer.addIIDFDatasetExplorerTab(explore_iidf_panel, e
				.getSelectedFile().getName());
		dataset_explorer.getDialog_load_dataset().setVisible(false);
	}

	@Override
	public void pathChanged(FSEEvent e) {
	}

	@Override
	public void selectionChanged(FSEEvent e) {
	}

}
